package com.umfwechat.h5.controller.store;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.umfwechat.common.code.RetCode;
import com.umfwechat.common.constant.CommonConsts;
import com.umfwechat.common.constant.ConfigConsts;
import com.umfwechat.common.constant.MsgConsts;
import com.umfwechat.common.constant.ValueConsts;
import com.umfwechat.common.prop.UmfPropModel;
import com.umfwechat.util.PropertyUtil;
import com.umfwechat.util.ReqMessageUtil;
import com.umfwechat.util.ResultJsonUtil;
import com.umfwechat.util.SaasPropertyUtil;
import com.umfwechat.util.StringUtil;
import com.umfwechat.util.WexinQrCodeUtil;
import com.umfwechat.wx.service.WeixinService;
import com.umpay.operation.model.qrConf.QrConf;
import com.umpay.operation.service.qrConf.remote.QrConfServiceRemote;
import com.umpay.sp.model.common.ReqMessage;
import com.umpay.sp.model.common.ResMessage;
import com.umpay.sp.model.saas.AppChannel;
import com.umpay.sp.model.saas.StoreInf;
import com.umpay.sp.service.saas.remote.AppChannelServiceRemote;
import com.umpay.sp.service.saas.remote.StoreInfServiceRemote;
import com.umpay.sp.service.sys.remote.UserServiceRemote;
import com.umpay.sp.xml.XmlData;
/**
 * @ClassName StoreController
 * @desc 选择店铺
 * @author liuchunyan
 * @date 2017-05-08上午11:58:18
 * @version 1.0
 */
@Controller
@RequestMapping("/h5/store/receiver")
public class StoreController {
	private static Logger logger = LoggerFactory.getLogger(StoreController.class);

	//查询第三方网站用户 ,查询用户详情接口
	@Resource
	private UserServiceRemote userServiceRemote_sp;
		
	//店铺部署详情列表接口
	@Resource
	private StoreInfServiceRemote storeInfServiceRemot;
	//渠道开通查询接口
	@Resource
	private AppChannelServiceRemote  appChannelServiceRemote;
	//二维码查询
	@Resource
	private QrConfServiceRemote qrConfServiceRemote;
	//调微信接口
	@Resource
	private WeixinService weixinService;
	//立马付产品编号
	private String productId_impay = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.PRODUCTID_IMPAY);
	// 收入结算产品编号
	private	String advanceSettle_productId = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.PRODUCTID_ADVANCESETTLE);
	
	/**
	 * desc:店铺初始化查询接口	
	 * 
	 * @param reqMessage
	 * @return ResMessage
	 * @throws 
	 * 	 */
	@RequestMapping(value="/getStoreByOpenid",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getStoreByOpenid(HttpServletRequest request, HttpServletResponse response) throws Exception{		
		logger.info("店铺初始化查询接口 开始");
		String openid = "";
		if(request.getSession().getAttribute("openid") != null){
			openid = request.getSession().getAttribute("openid").toString();//获取openid
		}
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(openid.isEmpty()){
			logger.info("openid获取失败！");
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "openid获取失败！");
			return JSONObject.fromObject(resultMap).toString();
		}
		
		try{
			//根据opendi查询第三方网站用户的用户编号
			ReqMessage reqMessage = ReqMessageUtil.makeSpReq();
			reqMessage.getReqMap().put("externaluserId", openid);
			logger.info("查询第三方网站用户接口 ：externaluserId=" + openid);
			ResMessage resMessage = userServiceRemote_sp.queryUserBind(reqMessage);
			if(!resMessage.getRetCode().equals(RetCode.SUCCESS)){
				logger.info("查询第三方网站用户接口 ：数据库错误");
				resultMap.put("retCode", RetCode.FAIL);
				resultMap.put("retMsg", "查询第三方网站用户失败！");
				return JSONObject.fromObject(resultMap).toString();
			}
			if(resMessage.getValue("userId") == null){//未取到用户编号
				resultMap.put("retCode", RetCode.FAIL);
				resultMap.put("retMsg", "未获取到用户编号！");
				logger.info("查询第三方网站用户接口 ：未获取到用户编号");
				return JSONObject.fromObject(resultMap).toString();
			}
			String userId = "";
			if(resMessage.getResMap().get("userId") != null){
				userId = resMessage.getResMap().get("userId").toString();//用户编号
			}
			
			//查询用户详情接口
			ReqMessage reqMess = ReqMessageUtil.makeSpReq();
			reqMess.getReqMap().put("userId", userId);
			logger.info("查询用户详情接口 ：reqMess=" + reqMess);
			ResMessage resMess = userServiceRemote_sp.getUserByIdSaaS(reqMess);
			Map userMap = (Map)resMess.getValue(XmlData.MER_USER);
			if(!resMess.getRetCode().equals(RetCode.SUCCESS)){
				resultMap.put("retCode", RetCode.FAIL);
				resultMap.put("retMsg", "查询用户详情失败！");
				logger.info("查询用户详情接口 ：数据库错误");
				return JSONObject.fromObject(resultMap).toString();
			}
			if(userMap.get("userId") == null){//未取到用户详情
				resultMap.put("retCode", RetCode.FAIL);
				resultMap.put("retMsg", "未获取到用户详情！");
				logger.info("查询用户详情接口 ：未获取到用户详情");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			if(userMap.get("merId") == null){//未取到商户号
				resultMap.put("retCode", RetCode.FAIL);
				resultMap.put("retMsg", "未获取到用户详情！");
				logger.info("查询用户详情接口 ：未获取到商户号");
				return JSONObject.fromObject(resultMap).toString();
			}
			String merId = "";
			if(userMap.get("merId") != null){
				merId = userMap.get("merId").toString().trim();
			}
			
			//身份
			Integer rank = null;
			if(userMap.get(CommonConsts.RANK) != null){
				rank = Integer.parseInt(userMap.get(CommonConsts.RANK).toString());
			}
			
			//店铺部署详情列表接口
			ReqMessage reqM = ReqMessageUtil.makeSpReq();
			//收银员
			if(rank == 1){
				reqM.getReqMap().put(CommonConsts.STOREID, merId);
				//收款人
			}else if(rank == 0){
				reqM.getReqMap().put(CommonConsts.MERID, merId);
			}
			//06:待修改，07:自动审核通过待审查，08:复审开通成功，09:待复查
			reqM.getReqMap().put("registerProcsStates", ValueConsts.OPEN_STORE_SUC_STATE);
			logger.info("店铺部署详情列表接口 ：reqM=" + reqM);
			ResMessage resM = storeInfServiceRemot.getStoreInfList(reqM);
			logger.info("店铺部署详情列表接口 ：resM=" + resM);
			if(!resM.getRetCode().equals(RetCode.SUCCESS)){
				resultMap.put("retCode", RetCode.FAIL);
				resultMap.put("retMsg", "数据库错误！");
				logger.info("店铺部署详情列表接口 ：数据库错误");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			@SuppressWarnings("unchecked")
			List<StoreInf> storeList = (List<StoreInf>) resM.getResMap().get("storeInfList");
			if(storeList == null || storeList.size() == 0){
				resultMap.put("retCode", RetCode.FAIL);
				resultMap.put("retMsg", "用户未开通店铺！");
				logger.info("店铺部署详情列表接口 ：结果不存在");
				return JSONObject.fromObject(resultMap).toString();
			}
			
			//编辑输出
			List<Map<String,Object>> resultList = new ArrayList<Map<String,Object>>();
			for(StoreInf storeInf : storeList){
				Map<String,Object> dataMap = new HashMap<String,Object>();
				//主商户号
				dataMap.put(CommonConsts.MERID, storeInf.getMerId());
				//店铺编号
				dataMap.put(CommonConsts.STOREID, storeInf.getStoreId());
				//店铺名称
				dataMap.put(CommonConsts.STORE_SHORT_NAME, storeInf.getStoreShortName());
				//是否是默认店铺
				dataMap.put(CommonConsts.DEFULTSTORE, storeInf.getDefultStore());
				//报备编号
				dataMap.put(CommonConsts.POS_REPORT_ID, storeInf.getPosReportId());
				resultList.add(dataMap);
			}
			
			resultMap.put("retCode", RetCode.SUCCESS);
			resultMap.put("retMsg", "获取店铺列表成功");
			resultMap.put("storeList", resultList);
			return JSONObject.fromObject(resultMap).toString();
			
		} catch (Exception e) {
			logger.info("获取店铺列表失败",e);
			resultMap.put("retCode", RetCode.FAIL);
			resultMap.put("retMsg", "获取店铺列表失败");
			return JSONObject.fromObject(resultMap).toString();
		}
	}
	
	/**
	 * desc:选择默认店铺接口
	 * 
	 * @param reqMessage
	 * @return ResMessage
	 * @throws 
	 * 	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value="/updateStore",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String updateStore(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String storeId = request.getParameter("storeId");
		String openid = "";
		if(request.getSession().getAttribute("openid") != null){
			openid = request.getSession().getAttribute("openid").toString();//获取openid
		}
		
		if(StringUtil.isEmpty(openid)){
			logger.info("openid获取失败！");
			return failureResult(RetCode.FAIL,"openid获取失败！");
		}
		if(StringUtil.isEmpty(storeId)){
			logger.info("缺少参数！");
			return failureResult(RetCode.FAIL,"缺少参数！");
		}
		
		ReqMessage reqMessage = ReqMessageUtil.makeSpReq();
		reqMessage.put("externaluserId", openid);
		//1查询第三方网站用户
		ResMessage userBind  = userServiceRemote_sp.queryUserBind(reqMessage);
		if(null==userBind.getValue("userId")){
			logger.info("未获取到用户的userId！");
			return failureResult(RetCode.FAIL,"未获取到用户的userId！");
		}	
		//2查询用户详情接口
		reqMessage.put(XmlData.USERID,userBind.getValue("userId").toString());
		ResMessage userInfo = userServiceRemote_sp.getUserByIdSaaS(reqMessage);
		
		Map<String,Object> user = null;
		if(null!=userInfo&&userInfo.getRetCode().equals("0000")){
			user = (Map<String,Object>) userInfo.getValue("merUser");
		}else{
			logger.info("未获取到用户信息！");
			return failureResult(RetCode.FAIL,userInfo.getRetMsg());
		}
		String merId = StringUtil.trim(user.get("merId").toString());
		
		//修改默认商铺
		ReqMessage req = ReqMessageUtil.makeSpReq();
		req.put(XmlData.MERID, merId);
		ResMessage storeResMessage = storeInfServiceRemot.getStoreInfList(req);
		if(!storeResMessage.getRetCode().equals(RetCode.SUCCESS)){
			logger.info("用户未开通商铺:查询数据库错误");
			return failureResult(RetCode.FAIL,"用户未开通商铺！");
		}
		
		List<StoreInf> storeInfList = (List<StoreInf>)storeResMessage.getValue(XmlData.STORE_INF_LIST);
		if(storeInfList != null && storeInfList.size() > 0){
			StoreInf oldDefultStoreInfo = null;
			StoreInf newDefultStoreInfo = null;
			for(StoreInf storeInf : storeInfList){
				if(storeInf.getDefultStore().equals("0")){//原默认店铺
					oldDefultStoreInfo = storeInf;
					oldDefultStoreInfo.setDefultStore("1");
					oldDefultStoreInfo.setIntime(null);
					oldDefultStoreInfo.setModtime(null);
				}
				if(storeInf.getStoreId().equals(storeId)){//新
					newDefultStoreInfo = storeInf;
					newDefultStoreInfo.setDefultStore("0");
					newDefultStoreInfo.setIntime(null);
					newDefultStoreInfo.setModtime(null);
				}
			}
			if(oldDefultStoreInfo == null){
				logger.info("没有查询到原默认店铺信息，修改默认商铺失败！");
				return failureResult(RetCode.FAIL,"修改默认商铺失败！");
			}
			if(newDefultStoreInfo == null){
				logger.info("没有查询到新默认店铺信息，修改默认商铺失败！");
				return failureResult(RetCode.FAIL,"修改默认商铺失败！");
			}
			Map<String, String> oldParamMap = SaasPropertyUtil.javaBeanToMap(oldDefultStoreInfo);
			Map<String, String> newParamMap = SaasPropertyUtil.javaBeanToMap(newDefultStoreInfo);
			ResMessage resMessage = storeInfServiceRemot.updateDefultStore(oldParamMap, newParamMap);
			if(resMessage.getRetCode().equals(RetCode.SUCCESS)){
				logger.info("修改默认商铺成功！");
				return successResult(RetCode.SUCCESS,"修改默认商铺成功！",null,null);
			}
		}
		
		logger.info("修改默认商铺失败！");
		return failureResult(RetCode.FAIL,"修改默认商铺失败！");
	}
	
	/**
	 * 获取渠道开通产品列表 (产品商店页面)
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getProductList",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getProductList(String merId,String storeId,HttpServletRequest request, HttpServletResponse response){
		logger.info("【获取产品商店信息】开始");
		if(StringUtil.isEmpty(merId)){
			logger.info("获取主商户编号失败！");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433002);
		}
		if(StringUtil.isEmpty(storeId)){
			logger.info("获取店铺编号失败！");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433002);
		}
		
		String productId_add = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.PRODUCTID_IMPAY_ADDED);
		String productId = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.PRODUCTID_IMPAY);
		// 立马付渠道开通列表返回查询接口
		ReqMessage req = ReqMessageUtil.makeSpReq();
		req.getReqMap().put(CommonConsts.APPSYSID,storeId);
		req.getReqMap().put("productIds","'"+productId+"','"+productId_add+"'");
		ResMessage res=null;
		try{
			logger.info("【根据主键查询渠道表接口】输入参数：ReqMessage = " + req);
			res =appChannelServiceRemote.getAppChannelListByProductids(req);
			logger.info("【根据主键查询渠道表接口】输出参数：ResMessage =  " + res);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("【渠道开通列表返回查询接口异常】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433002);
		}
		if (!RetCode.SUCCESS.equals(res.getRetCode())) {
			logger.info("【渠道开通列表返回查询接口失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433002);
		}
		List<Map<String,String>> resList = new ArrayList<Map<String,String>>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if(RetCode.SUCCESS.equals(res.getRetCode())){
			//获取查询到的渠道信息
			List<AppChannel> appChannellist = (List<AppChannel>)res.getValue("appChannelList");
			if(appChannellist==null||appChannellist.isEmpty()){
				Map<String,String> map = new HashMap<String, String>();
				map.put(CommonConsts.STATE,ValueConsts.STATE_FOUR);
				resList.add(map);
				logger.info("获取立马付渠道开通产品列表为空!");
				resultMap.put(CommonConsts.PRODUCTVO, resList);
			}else{
				AppChannel appChannel = appChannellist.get(0);
				Map<String,String> map = new HashMap<String, String>();
				map.put(CommonConsts.PRODUCTID, appChannel.getProductId());
				map.put(CommonConsts.PRODUCTNAME,appChannel.getAppName());
				map.put(CommonConsts.STATE, appChannel.getState().toString());
				map.put(CommonConsts.PRODUCTTYPE,ValueConsts.STATE_ONE);
				resList.add(map);
				logger.info("获取立马付渠道开通产品列表成功!");
				resultMap.put(CommonConsts.PRODUCTVO, resList);
			}
			
		}
		
		// 收入结算渠道开通列表返回查询接口
		ReqMessage reqM = ReqMessageUtil.makeSpReq();
		reqM.getReqMap().put(CommonConsts.APPSYSID,merId);
		reqM.getReqMap().put(CommonConsts.PRODUCTID,advanceSettle_productId);
		ResMessage resM=null;
		try{
			logger.info("【根据主键查询收入结算渠道表接口】输入参数：ReqMessage = " + reqM);
			resM =appChannelServiceRemote.queryByAppSysId(reqM);
			logger.info("【根据主键查询收入结算渠道表接口】输出参数：ResMessage =  " + resM);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("【根据主键查询收入结算渠道表接口异常】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433093);
		}
		if (!RetCode.SUCCESS.equals(resM.getRetCode())&& !RetCode.MER_NOT_EXIST.equals(resM.getRetCode())) {
			logger.info("【根据主键查询收入结算渠道表接口失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433002);
		}
		if(RetCode.SUCCESS.equals(resM.getRetCode())){
			// 获取查询到的渠道信息
			AppChannel appChannel = (AppChannel) resM.getValue(CommonConsts.APPCHANNEL);
			Map<String,String> map = new HashMap<String, String>();
			map.put(CommonConsts.PRODUCTID, appChannel.getProductId());
			map.put(CommonConsts.PRODUCTNAME,appChannel.getAppName());
			map.put(CommonConsts.STATE, appChannel.getState().toString());
			map.put(CommonConsts.PRODUCTTYPE,ValueConsts.STATE_TWO);
			resList.add(map);
		    logger.info("获取收入结算渠道开通产品成功!");
		    resultMap.put(CommonConsts.PRODUCTVO, resList);
		}else{
			Map<String,String> map = new HashMap<String, String>();
			map.put(CommonConsts.PRODUCTID,advanceSettle_productId);
			map.put(CommonConsts.PRODUCTNAME,UmfPropModel.INSTANCE.getPropValue(ConfigConsts.MAINPRODUCTNAME_ADVANCESETTLE));
			map.put(CommonConsts.STATE,ValueConsts.APP_CHANNEL_STATE_FOUR);
			resList.add(map);
		    logger.info("收入结算渠道开通产品未签约!");
		    resultMap.put(CommonConsts.PRODUCTVO, resList);
		}
		return ResultJsonUtil.resultJson(RetCode.SUCCESS, MsgConsts.I00433100, resultMap);
	}
	
	/**
	 * 产品签约信息接口
	 * @param session
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getStore",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String productSign(HttpSession session,HttpServletRequest request, HttpServletResponse response) throws Exception{
		String appSysId = request.getParameter("storeId");
		logger.info("appSysId:"+appSysId);
		if(appSysId==null||appSysId.equals("")){
			logger.info("缺少appSysId！");
			return failureResult(RetCode.FAIL,"缺少参数！");
		}
		String productId_add = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.PRODUCTID_IMPAY_ADDED);
		String productId = UmfPropModel.INSTANCE.getPropValue(ConfigConsts.PRODUCTID_IMPAY);
		// 立马付渠道开通列表返回查询接口
		ReqMessage req = ReqMessageUtil.makeSpReq();
		req.getReqMap().put(CommonConsts.APPSYSID,appSysId);
		req.getReqMap().put("productIds","'"+productId+"','"+productId_add+"'");
		ResMessage res=null;
		try{
			logger.info("【渠道开通列表返回查询接口】输入参数：ReqMessage = " + req);
			res =appChannelServiceRemote.getAppChannelListByProductids(req);
			logger.info("【渠道开通列表返回查询接口】输出参数：ResMessage =  " + res);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("【渠道开通列表返回查询接口异常】");
			return failureResult(RetCode.FAIL, MsgConsts.E00433002);
		}
		
		if(RetCode.SUCCESS.equals(res.getRetCode())){
			//获取查询到的渠道信息
			List<AppChannel> appChannellist = (List<AppChannel>)res.getValue("appChannelList");
			if(appChannellist==null||appChannellist.isEmpty()){
				logger.info("获取产品开通状态失败!");
				return failureResult(RetCode.FAIL, MsgConsts.E00433002);
			}else{
				AppChannel appChannel = appChannellist.get(0);
				List<AppChannel> list = new ArrayList<AppChannel>();
				list.add(appChannel);
				return successResult(res.getRetCode(),res.getRetMsg(),"storeVo",list);
			}
			
		}else{
			//获取产品开通状态失败
			logger.info("获取产品开通状态失败!");
			return failureResult(RetCode.FAIL, MsgConsts.E00433002);
		}
		
	}
	
	/**
	 * 获取QrId (收款二维码)
	 * 
	 * @param session
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getQrIdByStoreId",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getQrIdByStoreId(HttpSession session,HttpServletRequest request, HttpServletResponse response) throws Exception{
		String storeId = request.getParameter("storeId");
		if(StringUtil.isEmpty(storeId)){
			logger.info("缺少storeId！");
			return failureResult(RetCode.FAIL,"缺少参数！");
		}
		com.umpay.operation.common.ReqMessage req = new com.umpay.operation.common.ReqMessage();
		req.put("merId", storeId);
		com.umpay.operation.common.ResMessage res = qrConfServiceRemote.queryQrConfByMerId(req);
		if(res.getRetCode().equals("0000")){
			@SuppressWarnings("unchecked")
			List<QrConf> list = (List<QrConf>) res.getValue("qrList");		
			if(list != null && list.size() > 0){
				QrConf qr = list.get(0);
				logger.info("收款二维码  QrId=" + qr.getQrId());
				return successResult(res.getRetCode(),res.getRetMsg(),"qrCode", qr.getQrId());
			}else{
				logger.info("此商铺没有绑定二维码！");
				return failureResult(RetCode.FAIL,"此商铺没有绑定二维码！");
			}
		}else if(res.getRetCode().equals(RetCode.NO_DATA)){
			logger.info("此商铺没有绑定二维码！");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433099);
		}else{
			logger.info("数据库错误");
			return failureResult(RetCode.FAIL,MsgConsts.E00433002);
		}		
	}	
	
	/**
	 * 获取收款二维码 (收款二维码)
	 * 
	 * @param session
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getStoreEwm",produces="text/html;charset=UTF-8")
	@ResponseBody
	public void getStoreQrCode(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String qrCode = request.getParameter("qrCode");
		String qrUrl = PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties","report.qr.url");
		String qrService = PropertyUtil.getStrValue("umfWeChatPlat_appConf.properties","report.qr.service");
		String qrCodeUrl = qrUrl + "?service=" + qrService + "&qr_code=" + qrCode;
		if(StringUtil.isEmpty(qrUrl) || StringUtil.isEmpty(qrService)){
			logger.info("二维码下载参数获取失败!");
			return;
		}
		logger.info("收款二维码  qrCodeUrl = " + qrCodeUrl);
		logger.info("收款二维码  qrCode = " + qrCode);
		WexinQrCodeUtil.encodeQrcode(qrCodeUrl, response, qrCode);
		return;
	}
	
	private String failureResult(String code,String message){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", false);
		resultMap.put("retCode", code);
		resultMap.put("retMsg", message);
		return JSONObject.fromObject(resultMap).toString();
	}
	private String successResult(String code,String message,String name,Object obj){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		resultMap.put("retCode", code);
		resultMap.put("retMsg", message);	
		if(name!=null&&obj!=null){
			resultMap.put(name, obj);
		}
		
		String str = JSONObject.fromObject(resultMap).toString();	
		return str;
	}
	
	/**
	 * 获取收入结算产品签约信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/getAdvanceSettleProduct",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getAdvanceSettleProduct(String amount,String merId,HttpServletRequest request, HttpServletResponse response) throws Exception{
		logger.info("【获取收入结算产品签约信息】开始");
		//String storeId = request.getParameter(CommonConsts.STOREID);
		/*if(StringUtil.isEmpty(merId)){
			logger.info("缺少主商户编号！");
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433002);
		}
		String url =UmfPropModel.INSTANCE.getPropValue(ConfigConsts.BORROW_RATE_SERVICE_CHARGE);
		Map<String, Object> params = new HashMap<String, Object>();
		ReqMessage re = ReqMessageUtil.makeSpReq();
		String rpid = re.getRpid();
		params.put(CommonConsts.RPID, rpid);
		params.put(CommonConsts.REQDATE, re.getReqDate());
		params.put(CommonConsts.REQTIME, re.getReqTime());
		params.put(CommonConsts.MERID,merId);
		params.put(CommonConsts.PRODUCTID,"P1540013");
		params.put("amount",amount);
		Map<String, Object> rescon=null;
		try {
			logger.info("【商户费率查询】httpPostForm2Xml请求数据：params"+params+ "访问路径url="+ url);	
			rescon = HttpUtil.httpPostForm2Xml(url, params);
			logger.info("【商户费率查询】httpPostForm2Xml返回结果：",rescon);
			
		}catch (Exception e) {
			logger.error("调用商户费率查询接口异常"+e);
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433102);
		}
		if(null==rescon||StringUtil.isEmpty((String) rescon.get(CommonConsts.RETCODE))){
			logger.info("【商户费率查询】失败,返回值rescon="+rescon);
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433102);
		}
		if(!RetCode.SUCCESS.equals(StringUtil.trim(rescon.get(CommonConsts.RETCODE)))){
			logger.info("【商户费率查询】失败,返回码"+rescon.get(CommonConsts.RETCODE));
			return ResultJsonUtil.resultJson(RetCode.FAIL, MsgConsts.E00433102);
		}else{
			String feerate = StringUtil.trim(rescon.get(CommonConsts.FEERATE));
			Map<String, Object> map = new HashMap<String, Object>();
			map.put(CommonConsts.FEERATE,feerate);
			return ResultJsonUtil.resultJson(RetCode.SUCCESS,"",map);
		}*/
		String rateDate =UmfPropModel.INSTANCE.getPropValue(ConfigConsts.PAYMENT_ACC_FEE_DATE);
		String[] split = rateDate.split(CommonConsts.AND);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(CommonConsts.FEEVALUE,split[3]);
		return ResultJsonUtil.resultJson(RetCode.SUCCESS,"",map);
		
	}
	
	
	@RequestMapping(value="/logout",produces="text/html;charset=UTF-8")
	@ResponseBody
	public String logout(HttpServletRequest request, HttpServletResponse response){

		String openId = StringUtil.trim(request.getSession().getAttribute(CommonConsts.OPENID));
		//openId="o9Gdj1Z89rbDTqeWZN5NCCbPR00c";
		if(StringUtil.isEmpty(openId)){
			logger.info("session中获取openId为空");
			return ResultJsonUtil.resultJson(RetCode.FAIL,MsgConsts.E00433001);
		}
		
        //根据外部用户Id查询商户是否被人绑定
		ReqMessage reqMess = ReqMessageUtil.makeSpReq();
		reqMess.getReqMap().put(CommonConsts.EXTERNALUSERID, openId);
		reqMess.getReqMap().put(CommonConsts.STATE,ValueConsts.EXTERNAL_USER_NOMAL);
		ResMessage resMess=new ResMessage();
		try {
			logger.info("【查询第三方网站用户接口】输入参数 ：" + reqMess);
		    resMess = userServiceRemote_sp.queryUserBind(reqMess);
			logger.info("【查询第三方网站用户接口】输出参数 :" + resMess);
		} catch (Exception e) {
			logger.info("【查询第三方网站用户接口】异常"+e);
			return ResultJsonUtil.resultJson(RetCode.FAIL,MsgConsts.E00433002);
		}
		
		//第三方网站查询失败
		if(!RetCode.SUCCESS.equals(resMess.getRetCode())){
			logger.info("【查询第三方网站用户接口失败】");
			return ResultJsonUtil.resultJson(RetCode.FAIL,MsgConsts.E00433002);
		}
		
		String userId = StringUtil.trim(resMess.getValue(CommonConsts.USERID));
		
		ReqMessage reqMes = ReqMessageUtil.makeSpReq();
		reqMes.getReqMap().put(CommonConsts.EXTERNALUSERID,openId);
		reqMes.getReqMap().put(CommonConsts.USERID,userId);
		reqMes.getReqMap().put(CommonConsts.STATE,ValueConsts.EXTERNAL_USER_LOG_OFF);
		ResMessage resMes=new ResMessage();
		try {
			logger.info("【解绑第三方网站用户接口】输入参数 ：=" + reqMes);
			resMes = userServiceRemote_sp.unBindUser(reqMes);
			logger.info("【解绑第三方网站用户接口】输出参数：=" +resMes);
		} catch (Exception e) {
			logger.info("【解绑第三方网站用户接口】异常"+e);
			return ResultJsonUtil.resultJson(RetCode.FAIL,MsgConsts.E00433002);
		}
		
		if(!RetCode.SUCCESS.equals(resMes.getRetCode())){
			logger.info("【解绑第三方网站用户接口失败】 ");
			return ResultJsonUtil.resultJson(RetCode.FAIL,MsgConsts.E00433030);
		}else{
			boolean group = weixinService.addUserGroup(openId,0);
			logger.info("此微信openId="+openId+"打成无角色标签flag="+group);
		}
		return ResultJsonUtil.resultJson(RetCode.SUCCESS,CommonConsts.SPACE);
		
	}	
	
}